package com.zq.多线程设计模式.EventDriven设计模式.EDA同步;

import java.util.HashMap;
import java.util.Map;

/*
 *  EventDispatcher 不是一个线程安全的类
 * */
public class EventDispatcher implements DynamicRouter<Message> {

    private final Map<Class<? extends Message>, Channel> routertable;

    public EventDispatcher() {
        this.routertable = new HashMap<>();
    }

    @Override
    public void registerChannel(Class<? extends Message> messageType, Channel<? extends Message> channel) {
        this.routertable.put(messageType, channel);
    }

    @Override
    public void dispatch(Message message) {
        if (routertable.containsKey(message.getType())) {
            routertable.get(message.getType()).dispatch(message);
        } else {
            throw new MessageMatcherException("Can't match the channel for [ " + message.getType() + "] type");
        }
    }
}
